package com.zxk.crawler.downloader;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.AbstractDownloader;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.PlainText;
import us.codecraft.webmagic.utils.CharsetUtils;
import us.codecraft.webmagic.utils.HttpClientUtils;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @Description TODO
 * @Author zxk
 * @DATE 2023/4/7 13:47
 * @Version 1.0
 **/
@Slf4j
public class UnirestDownLoader extends AbstractDownloader {
    private boolean responseHeader = true;
    @Override
    public Page download(Request request, Task task) {
        Page page = Page.fail();
        try {
            String url = request.getUrl();
            HttpResponse<String> response = Unirest.get(url)
                    .header("User-Agent", "Apifox/1.0.0 (https://www.apifox.cn)")
                    .header("Accept", "*/*")
                    .header("Host", "www.ibiquge.la")
                    .header("Connection", "keep-alive")
                    .asString();
//            page = handleResponse(request, request.getCharset() != null ? request.getCharset() : task.getSite().getCharset(), response, task);
//        System.out.println(JSON.toJSONString(response));
            String body = response.getBody();
            Html html = new Html(body, url);
            page.setHtml(html);
            onSuccess(request);
            log.info("downloading page success {}", request.getUrl());
            return page;
        } catch (UnirestException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void setThread(int threadNum) {

    }

    protected Page handleResponse(Request request, String charset, org.apache.http.HttpResponse httpResponse, Task task) throws IOException {
        byte[] bytes = IOUtils.toByteArray(httpResponse.getEntity().getContent());
        String contentType = httpResponse.getEntity().getContentType() == null ? "" : httpResponse.getEntity().getContentType().getValue();
        Page page = new Page();
        page.setBytes(bytes);
        if (!request.isBinaryContent()){
            if (charset == null) {
                charset = getHtmlCharset(contentType, bytes);
            }
            page.setCharset(charset);
            page.setRawText(new String(bytes, charset));
        }
        page.setUrl(new PlainText(request.getUrl()));
        page.setRequest(request);
        page.setStatusCode(httpResponse.getStatusLine().getStatusCode());
        page.setDownloadSuccess(true);
        if (responseHeader) {
            page.setHeaders(HttpClientUtils.convertHeaders(httpResponse.getAllHeaders()));
        }
        return page;
    }

    private String getHtmlCharset(String contentType, byte[] contentBytes) throws IOException {
        String charset = CharsetUtils.detectCharset(contentType, contentBytes);
        if (charset == null) {
            charset = Charset.defaultCharset().name();
            log.warn("Charset autodetect failed, use {} as charset. Please specify charset in Site.setCharset()", Charset.defaultCharset());
        }
        return charset;
    }
}
